package voice

import (
	"encoding/json"
	"fmt"
	http_label_center "gobase/http_work_flow/label_center"
	"time"
)

const (
	TypeText文本 = 1
	TypeText字幕 = 2
	TypeText歌词 = 3
)

type ReqDownloadVoiceFile struct {
	TaskId   string `form:"task_id" json:"TaskId"`     //任务ID
	TypeText int8   `form:"type_text" json:"TypeText"` //结果类型
	FileLink string `json:"FileLink"`                  //文件地址

	Log http_label_center.Log `json:"Log,omitempty"` //追踪日志
}

type ReqResultVoiceFileText struct {
	TaskId string                `form:"task_id" json:"TaskId"` //任务ID
	Log    http_label_center.Log `json:"Log,omitempty"`         //追踪日志
}

func getHMSM(millis int64) string {
	duration := time.Duration(millis) * time.Millisecond
	t := time.Time{}.Add(duration)
	return fmt.Sprintf("%02d:%02d:%02d,%03d", t.Hour(), t.Minute(), t.Second(), t.Nanosecond()/1000000)
}

func getMSM(millis int64) string {
	duration := time.Duration(millis) * time.Millisecond
	t := time.Time{}.Add(duration)
	return fmt.Sprintf("[%02d:%02d.%02d]", t.Minute(), t.Second(), t.Nanosecond()/1000000)
}

type RedisReqRespVoiceFile struct {
	Req  ReqVoiceFile
	Resp RespVoiceFile

	FileName string `json:"FileName"`
	Is8K     bool   `json:"Is8K"`
}

// 序列化
func (m *RedisReqRespVoiceFile) MarshalBinary() (data []byte, err error) {
	fmt.Println("MarshalBinary")
	return json.Marshal(m)
}

// 反序列化
func (m *RedisReqRespVoiceFile) UnmarshalBinary(data []byte) error {
	fmt.Println("UnmarshalBinary")
	return json.Unmarshal(data, m)

}

type RedisResultVoiceFile struct {
	ReqRespVoiceFile RedisReqRespVoiceFile
	RespResult       RespVoiceFileResult

	JsonResult string

	Txt string
	Srt string
	Lrc string
}

// 序列化
func (m *RedisResultVoiceFile) MarshalBinary() (data []byte, err error) {
	fmt.Println("MarshalBinary")
	return json.Marshal(m)
}

// 反序列化
func (m *RedisResultVoiceFile) UnmarshalBinary(data []byte) error {
	fmt.Println("UnmarshalBinary")
	return json.Unmarshal(data, m)

}

// /////////////////////////////////////////

func (t Results) GetText() string {
	var txt string
	for i := 0; i < len(t); i++ {
		txt += fmt.Sprintf("%s\r\n", t[i].Text)
	}
	return txt
}

func (t Results) GetStrJsonResult() string {
	bytResults, _ := json.Marshal(&t)
	return string(bytResults)
}

func (t Results) GetLyric() string {
	var lyric string
	for i := 0; i < len(t); i++ {
		lyric += fmt.Sprintf("%s%s\r\n", getMSM(t[i].BeginTime), t[i].Text)
	}
	return lyric
}

func (t Results) GetSRT() string {
	var srt string
	for i := 0; i < len(t); i++ {
		srt += fmt.Sprintf("%d\r\n%s --> %s\r\n%s\r\n", i+1, getHMSM(t[i].BeginTime), getHMSM(t[i].EndTime), t[i].Text)
	}
	return srt
}
